package csci4448.android.workouttracker;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static android.provider.BaseColumns._ID;

public class SessionModel extends SQLiteOpenHelper {

	private static final int version = 3;
	Date date = new Date();
	private SessionFactory _sessionFactory;
	public SessionModel(Context context)
	{
		super(context, "sessions.db", null, version);
		_sessionFactory = new SessionFactory();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// _ID is a self incrementing id Integer
		// session is the sessionName String
		// startTime and endTime is a String
		// timeElapsed is an INTEGER and represents the elapsed seconds
		// distance is an REAL and represents the distance covered in meters
		db.execSQL("CREATE TABLE sessions (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
											" session TEXT NOT NULL," +
											" startTime TEXT NOT NULL," +
											" endTime TEXT NOT NULL," +
											" timeElapsed INTEGER NOT NULL," +
											" distance REAL NOT NULL);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS sessions");
		onCreate(db);
	}
	
	private Cursor queryDB(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
	{
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
		return cursor;
	}
	
	public Session getSessionById(Integer sessionID) throws Exception
	{
		Cursor cursor = queryDB("sessions", new String[] { _ID, "timeElapsed", "distance", "startTime", "endTime", "session"}, "_ID=?", new String[] { sessionID.toString() }, null, null, null);
		while (cursor.moveToNext() && cursor.getInt(0) == sessionID)
		{
			return _sessionFactory.createSession(cursor.getInt(0), cursor.getLong(1), cursor.getDouble(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
		}
		throw new Exception("No session by that ID");
	}
	public List<Session> getSessions() throws Exception
	{
		List<Session> Sessions = new ArrayList<Session>();
		Cursor cursor = queryDB("sessions", new String[] { _ID, "timeElapsed", "distance", "startTime", "endTime", "session"}, null, null, null, null, null);
		while (cursor.moveToNext())
		{
			Sessions.add(_sessionFactory.createSession(cursor.getInt(0), cursor.getLong(1), cursor.getDouble(2), cursor.getString(3), cursor.getString(4), cursor.getString(5)));
		}
		return Sessions;
	}

	public void addSession(Session sessionToAdd) {
		// create the time strings
		String startString = sessionToAdd.getStartTime();
		String endString = sessionToAdd.getEndTime();
		
		// open the database to write to
		SQLiteDatabase db = this.getWritableDatabase();
		
		// create the content values
		db.beginTransaction();
		ContentValues values = new ContentValues();
		values.put("session", sessionToAdd.getSessionName());
		values.put("startTime", startString);
		values.put("endTime", endString);
		values.put("timeElapsed", sessionToAdd.getDurationSeconds());
		values.put("distance", sessionToAdd.getStorageDistance());
		
		// insert the values into the database
		db.insert("sessions", null, values);
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}
}